/*
 *  Copyright 2012.
 * 
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 * 
 *       http://www.apache.org/licenses/LICENSE-2.0
 * 
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */

package org.opu.queryhandler.n.client.ui;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JMenuBar;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTabbedPane;
import javax.swing.JToolBar;
import org.opu.queryhandler.n.client.EventBus;
import org.opu.queryhandler.n.client.ui.widgets.SqlEditPane;
import org.opu.queryhandler.n.util.ConfigPath;
import org.opu.queryhandler.n.util.FileIO;
import org.opu.queryhandler.n.util.JProperties;
import org.opu.queryhandler.n.util.Logger;
import org.opu.queryhandler.n.util.ResourceHandle;

/**
 *
 */
public class MainFrame extends JFrame {

    private static final long serialVersionUID = 973711332096920599L;

    public static final String LOCATION = "location";
    public static final String MAXIMIZE = "maximize";
    public static final String SIZE = "size";

    public static final String FREEZE_LAYOUT = "freeze-layout";
    public static final String CLOSE_BUTTON_ENABLE = "close-button-enable";
    public static final String THEME = "theme";
    public static final String LAF = "laf";

    /** Properties map with all properties of window. */
    private JProperties configs;
    private Logger logger = Logger.getInstance();

    private SqlEditPane sql;
    private JTabbedPane tabbedPane;

    private JMenuBar menuBar;
    private JToolBar toolBar;

    public MainFrame(JProperties configs, final EventBus eventBus) {
        this.configs = configs;
        this.tabbedPane = new JTabbedPane();
        this.sql = new SqlEditPane(tabbedPane);

        eventBus.setTextResource(sql);
        eventBus.getExitAction().addActionListeners(getFinalActions());
        initFrame(configs);

        menuBar = new MenuBar(eventBus, this);
        toolBar = new ToolbarMaker(eventBus);


        setJMenuBar(menuBar);
        Container pane = getContentPane();
        pane.add(toolBar, BorderLayout.NORTH);
        JSplitPane sp = new JSplitPane(JSplitPane.VERTICAL_SPLIT, tabbedPane, sql);
        sp.setDividerLocation(250);
        pane.add(sp);
        setIconImage(ResourceHandle.getWindowImage());

        addWindowListener(new WindowAdapter() {

            @Override
            public void windowClosing(WindowEvent e) {
                eventBus.getExitAction().actionPerformed(null);
            }

        });
    }

    public void storeLookAndFeel(String className){
        configs.setProperty(LAF, className);
    }

    private void initFrame(JProperties prop) {
        if (prop == null) {
            configs = new JProperties();

            try {
                FileIO.store(ConfigPath.getInstance().getFrameProp(), configs, "");
            } catch (IOException ex) {
                logger.info(this, "When load properties - " + ex);
            }
        } else {
            configs = prop;
        }

        setSize(configs.getDimension(SIZE, new Dimension(700, 560)));
        Point point = configs.getPoint(LOCATION);
        if (point != null) {
            setLocation(point);
        } else {
            setLocationRelativeTo(null);
        }

        setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
        setTitle(ResourceHandle.getApplicationTitle());
    }

    private List<ActionListener> getFinalActions() {
        return Arrays.asList(new ActionListener[]{new ActionListener() {
            @Override public void actionPerformed(ActionEvent e) {
                File file = ConfigPath.getInstance().getFrameProp();

                configs.setPoint(LOCATION, getLocation());
                configs.setDimension(SIZE, getSize());
                configs.setBoolean(MAXIMIZE, isMaximumSizeSet());

                try {
                    FileIO.store(file, configs, "window");
                } catch (IOException ex) {
                    logger.error(MainFrame.class, "When store properties - " + ex);
                }
            }
        }});
    }
}
